<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <!-- <el-button type="primary" @click="back">{{$t('all.confirm')}}</el-button> -->
                        <!-- <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">{{$t('all.add')}}</el-button> -->
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="search.contract_code" :placeholder="$t('sale.sale.code')" clearable @keyup.enter="upsearch"></el-input>
                            <el-input v-model="search.customer_name" :placeholder="$t('sale.client.name')" clearable @keyup.enter="upsearch"></el-input>
                            <el-select v-model="search.status" filterable :placeholder="$t('all.checkstatus')">
                                <el-option :label="$t('all.awit')" :value="0" />
                                <el-option :label="$t('all.checkok')" :value="1" />
                                <el-option :label="$t('all.checkno')" :value="2" />
                            </el-select>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" highlight-current-row @row-click="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                        <!-- <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column> -->
                        <el-table-column width="1"></el-table-column>
                        <el-table-column type="index" :label="$t('all.sort')" align="center" width="60"></el-table-column>
                        <el-table-column :label="$t('sale.sale.code')" prop="contract_code" align="center" width="100"></el-table-column>
                        <el-table-column :label="$t('sale.client.client')" prop="customer.customer_name" align="center"></el-table-column>
                        <el-table-column :label="$t('goods.goods.name')" prop="goods.goods_spec" align="center" width="130">
                            <template #default="scope">
                                <div v-for="item in scope.row.product" :key="item.id">
                                    {{item.goods&&item.goods.goods_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column :label="$t('all.number')" prop="cate_name" align="center" width="80">
                            <template #default="scope">
                                <div v-for="item in scope.row.product" :key="item.id">
                                    {{item.sale_num}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column :label="$t('all.remark')" prop="remark" align="center"></el-table-column>
                        <el-table-column :label="$t('sale.sale.date')" prop="open_single" align="center"></el-table-column>
                        <el-table-column :label="$t('sale.sale.leadtime')" prop="deliver_date" align="center"></el-table-column>
                        <!-- <el-table-column label="投产进度" prop="cate_name" align="center"></el-table-column>
                        <el-table-column label="出库进度" prop="cate_name" align="center"></el-table-column> -->
                        <el-table-column :label="$t('sale.sale.money')" prop="contract_price" align="center"></el-table-column>
                        <el-table-column :label="$t('sale.sale.receipts')" prop="cate_name" align="center"></el-table-column>
                        <!-- <el-table-column label="累计支出" prop="cate_name" align="center"></el-table-column>
                        <el-table-column label="应收款" prop="cate_name" align="center"></el-table-column>
                        <el-table-column label="已开票金额" prop="cate_name" align="center" width="100"></el-table-column> -->
                        <el-table-column :label="$t('sale.sale.istax')" prop="cate_name" align="center" width="110">
                            <template #default="scope">
                                <!-- <el-tag :type="scope.row.is_tax == 1 ? 'success' : 'danger'">{{scope.row.is_tax == 1 ? '含税' : '不含税'}}</el-tag> -->
                                <el-tag v-if="scope.row.is_tax == 1" type="success">{{$t('sale.sale.tax')}}</el-tag><!-- 含税 -->
                                <el-tag v-if="scope.row.is_tax == 0" type="danger">{{$t('sale.sale.untax')}}</el-tag><!-- 不含税 -->
                            </template>
                        </el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">{{$t('all.cancel')}}</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
    props:{
        query: { type: Object, default: () => ({}) },
    },
    components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: this.$t('production.selectsale'),
				edit: '编辑销售',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			apiObj: this.$API.contract.contract.list,
			selection: [],
			search: {
				contract_code: null,
                customer_name:null,
                status:null,
                contract_status:null,
			},
            dialog: {
				save: false,
			},
		}
	},
	mounted() {
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        // 选择事件
        selectionChange(selection) {
            this.$emit('success', selection)
            this.visible = false;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.type)
            this.visible = false;
        },
        //销售新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新销售新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
	}
}
</script>

<style></style>
